import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_CAPACITY = 10;
    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public void push(int val) {
        if(this.usedSize == this.elem.length) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[this.usedSize++] = val;
    }
    public int pop() {
        if(this.isEmpty()) {
            throw new NullPointerException("栈已空");
        }
        int oldVal = this.elem[usedSize-1];
        usedSize--;
        return oldVal;
    }
    public int peek() {
        //瞄一眼
        if(this.isEmpty()) {
            throw new NullPointerException("栈已空");
        }
        return this.elem[this.usedSize-1];
    }
    public boolean isEmpty() {
        return this.usedSize == 0;
    }
}
